package org.lisa.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.lisa.crm.common.util.MathUtil;
import org.lisa.crm.common.util.Result;
import org.lisa.crm.dao.CustomerDao;
import org.lisa.crm.dao.UserDao;
import org.lisa.crm.model.Customer;
import org.lisa.crm.model.CustomerOrder;
import org.lisa.crm.model.DataDic;
import org.lisa.crm.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2018/1/15.
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private UserDao userDao;


    @Override
    public  List<Customer> queryAllCustomers() {

        return userDao.queryAllCustomers();
    }

    @Override
    public Map<String, Object> queryCustomersByParams(Customer customer) {
       
        Map<String,Object> map=new HashMap<>();
        /*
        与model中的属性保持一致
         */
        map.put("isValid",1);
        map.put("id",customer.getId());
        List<Customer> list = customerDao.find(map);
        
        //处理分页
        PageHelper.startPage(customer.getPage(),customer.getRows());

        //创建一个包装了分页信息的分页对象,将list作为入参
        PageInfo<Customer> info = new PageInfo<>(list);

        //创建一个map将分页信息也放进去
        Map<String,Object> res=new HashMap<>();
        res.put("total",info.getTotal());
        res.put("rows",info.getList());

        return res;

    }


    @Override
    public Result saveCustomer(Customer customer){
        customer.setCreateDate(new Date());
        customer.setIsValid(1);
        customer.setKhno(MathUtil.genereateKhCode());
        int res=customerDao.saveSte(customer);

        if(res == 1){
            return Result.success("保存成功");
        }

        return Result.fail("保存失败");

    }

    @Override
    public Result updateCustomer(Customer customer){
        customer.setUpdateDate(new Date());
        int i=customerDao.updateSte(customer);
        if(i == 1){
            return Result.success("修改成功");
        }

        return Result.fail("修改失败");

    }

    @Override
    public Result deleteCustomer(int id){
        int i=customerDao.delete(id);
        if(i == 1){
            return Result.success("删除成功");
        }

        return Result.fail("删除失败");
    }

    @Override
    public List<DataDic> queryDataDicValueByDataDicName(String dataDicName) {

        return customerDao.queryDataDicValueByDataDicName(dataDicName);

    }

    @Override
    public Customer queryCusBycId(int id) {
            return customerDao.get(id);
    }

}
